package sk.fiit.vppj.eventmanager.gui.administrator;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.TableRowSorter;

import sk.fiit.vppj.eventmanager.data.Project;


public final class ProjectsTable extends JPanel {
	private JButton nextPageButton = null;
	private JButton previousPageButton = null;
	private ProjectTableModel model = null;
	private ProjectTable projectPanel = null;
	private int page = 0;

	public ProjectsTable(ProjectTable judgeGeneralPanel) {
		this.projectPanel = judgeGeneralPanel;
		init();
	}

	private void init() {
		showTable();
		showPreviousPageButton();
		showNextPageButton();
	}

	private void showPreviousPageButton() {
		previousPageButton = new JButton( "Previous page" );
		previousPageButton.addActionListener( new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				page--;
				model.prepareData( page );
				if( page == 0 ) {
					previousPageButton.setVisible( false );
				}
				if( model.getRowCount() == model.getPagination() ) {
					nextPageButton.setVisible( true );
				}
				updateUI();
			}
		} );
		previousPageButton.setVisible( false );
		add( previousPageButton );
	}

	private void showNextPageButton() {
		this.nextPageButton = new JButton( "Next page" );
		this.nextPageButton.addActionListener( new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				page++;
				model.prepareData( page );
				if( model.getRowCount() == 0 ) {
					page--;
					model.prepareData( page );
					nextPageButton.setVisible( false );
					if( page != 0 ) {
						previousPageButton.setVisible( true );

					}
				}
				else if( model.getRowCount() < model.getPagination() ) {
					nextPageButton.setVisible( false );
					previousPageButton.setVisible( true );
				}
				else {
					previousPageButton.setVisible( true );
					nextPageButton.setVisible( true );
				}
				updateUI();
			}
		} );
		this.nextPageButton.setVisible( true );
		add( this.nextPageButton );
	}

	private void showTable() {

		final JTable table = new JTable();
		this.model = new ProjectTableModel();
		this.model.prepareData( page );
		table.setModel( this.model );
		table.setFillsViewportHeight( true );
		table.setRowSorter( new TableRowSorter<ProjectTableModel>( this.model ) );
		table.getColumnModel().getColumn( 0 ).setPreferredWidth( 5 );
		table.getColumnModel().getColumn( 1 ).setPreferredWidth( 130 );
		table.getColumnModel().getColumn( 2 ).setPreferredWidth( 50 );
		table.getColumnModel().getColumn( 3 ).setPreferredWidth( 40 );
		table.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );
		table.addMouseListener( new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				if( table.getSelectedRow() != -1 ) {
					projectPanel.showProjectData( new Project( Integer.parseInt( table.getValueAt( table.getSelectedRow(), 0 ).toString() ) ) );
				}
			}
		} );

		table.addKeyListener( new KeyListener() {

			@Override
			public void keyTyped(KeyEvent e) {

			}

			@Override
			public void keyReleased(KeyEvent e) {
				// TODO Auto-generated method stub
			}

			@Override
			public void keyPressed(KeyEvent e) {
				if( e.getKeyCode() == 10 ) {
					if( table.getSelectedRow() != -1 ) {
						projectPanel.showProjectData( new Project( Integer.parseInt( table.getValueAt( table.getSelectedRow(), 0 )
								.toString() ) ) );
					}
				}
			}
		} );

		JScrollPane scrollPane = new JScrollPane( table );
		add( scrollPane );
		updateUI();
	}
}
